package com.jeff.android.lab.jframework;

import android.content.Context;

import com.jeff.android.lab.jframework.app.BaseApplication;
import com.jeff.android.lab.jframework.app.NetworkChangeListener;

/**
 * @author: duff
 * @date: 2019/1/14
 * @since: 1.0.0
 */
public class RuntimeManager {

    private static RuntimeManager sInstance = null;

    private boolean initialized = false;

    private BaseApplication mApplication;
    private String mAppVersion;
    private String mAppTag;

    public static RuntimeManager instance() {
        if (null == sInstance) {
            synchronized (RuntimeManager.class) {
                sInstance = new RuntimeManager();
            }
        }
        return sInstance;
    }

    private RuntimeManager() {

    }

    public void init(BaseApplication application, String version, String tag) {
        mApplication = application;
        mAppVersion = version;
        mAppTag = tag;

        initialized = true;
    }

    private void assetInitialized() {
        if (!initialized)
            throw new RuntimeException("RuntimeManager is not initialized!");
    }

    public Context getAppContext() {
        assetInitialized();

        return mApplication.getApplicationContext();
    }

    public String getAppVersion() {
        assetInitialized();

        return mAppVersion;
    }

    public String getAppTag() {
        assetInitialized();

        return mAppTag;
    }

    public void registerNetworkChangeListener(NetworkChangeListener l) {
        if (mApplication != null)
            mApplication.addNetworkChangeListener(l);
    }

    public void unRegisterNetworkChangeListener(NetworkChangeListener l) {
        if (mApplication != null)
            mApplication.removeNetworkChangeListener(l);
    }

}
